/*
Copyright 2008-2010 Gephi
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
Website : http://www.gephi.org

This file is part of Gephi.

Gephi is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

Gephi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with Gephi.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.gephi.ui.generator.plugin;

import org.gephi.lib.validation.BetweenZeroAndOneValidator;
import org.gephi.lib.validation.PositiveNumberValidator;
import org.netbeans.validation.api.Problems;
import org.netbeans.validation.api.Validator;
import org.netbeans.validation.api.builtin.Validators;
import org.netbeans.validation.api.ui.ValidationGroup;
import org.netbeans.validation.api.ui.ValidationPanel;
import org.openide.util.NbBundle;

/**
 *
 * @author Mathieu Bastian
 */
public class WattsStrogatzPanel extends javax.swing.JPanel {

    /** Creates new form RandomGraphPanel */
    public WattsStrogatzPanel() {
        initComponents();
    }

    public static ValidationPanel createValidationPanel(WattsStrogatzPanel innerPanel) {
        ValidationPanel validationPanel = new ValidationPanel();
        if (innerPanel == null) {
            innerPanel = new WattsStrogatzPanel();
        }
        validationPanel.setInnerComponent(innerPanel);

        ValidationGroup group = validationPanel.getValidationGroup();

        //Node field
        group.add(innerPanel.nodeField, Validators.REQUIRE_NON_EMPTY_STRING,
                new PositiveNumberValidator());

        //Neighbor field
        group.add(innerPanel.neighborField, Validators.REQUIRE_NON_EMPTY_STRING,
                new NeighborValidator(innerPanel));

        //Edge field
        group.add(innerPanel.probabilityField, Validators.REQUIRE_NON_EMPTY_STRING,
                new BetweenZeroAndOneValidator());

        return validationPanel;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nodeLabel = new javax.swing.JLabel();
        edgeLabel = new javax.swing.JLabel();
        nodeField = new javax.swing.JTextField();
        neighborField = new javax.swing.JTextField();
        probabilityField = new javax.swing.JTextField();
        edgeLabel1 = new javax.swing.JLabel();

        nodeLabel.setText(org.openide.util.NbBundle.getMessage(WattsStrogatzPanel.class, "WattsStrogatzPanel.nodeLabel.text")); // NOI18N

        edgeLabel.setText(org.openide.util.NbBundle.getMessage(WattsStrogatzPanel.class, "WattsStrogatzPanel.edgeLabel.text")); // NOI18N

        nodeField.setText(org.openide.util.NbBundle.getMessage(WattsStrogatzPanel.class, "WattsStrogatzPanel.nodeField.text")); // NOI18N

        neighborField.setText(org.openide.util.NbBundle.getMessage(WattsStrogatzPanel.class, "WattsStrogatzPanel.neighborField.text")); // NOI18N

        probabilityField.setText(org.openide.util.NbBundle.getMessage(WattsStrogatzPanel.class, "WattsStrogatzPanel.probabilityField.text")); // NOI18N

        edgeLabel1.setText(org.openide.util.NbBundle.getMessage(WattsStrogatzPanel.class, "WattsStrogatzPanel.edgeLabel1.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(nodeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(edgeLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(edgeLabel1)))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nodeField, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
                    .addComponent(neighborField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                    .addComponent(probabilityField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nodeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nodeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(neighborField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(edgeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(probabilityField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(edgeLabel1))
                .addContainerGap(46, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel edgeLabel;
    private javax.swing.JLabel edgeLabel1;
    protected javax.swing.JTextField neighborField;
    protected javax.swing.JTextField nodeField;
    private javax.swing.JLabel nodeLabel;
    protected javax.swing.JTextField probabilityField;
    // End of variables declaration//GEN-END:variables

    private static class NeighborValidator implements Validator<String> {

        private WattsStrogatzPanel innerPanel;

        public NeighborValidator(WattsStrogatzPanel innerPanel) {
            this.innerPanel = innerPanel;
        }

        @Override
        public boolean validate(Problems problems, String compName, String model) {
            boolean result = false;
            int limit = 0;
            try {
                Integer i = Integer.parseInt(innerPanel.nodeField.getText());
                limit = i / 2;
                int neighbor = Integer.parseInt(model);
                result = neighbor >= 2 && neighbor <= limit;
            } catch (Exception e) {
            }
            if (!result) {
                String message = NbBundle.getMessage(WattsStrogatzPanel.class,
                        "WattsStrogatzPanel.NeigborValidator.problem", model, String.valueOf(limit));
                problems.add(message);
            }
            return result;
        }
    }
}
